# -*- coding:utf-8 -*-
# @Time : 2020/6/24 17:39
# @Author : Administrator
# @File : authentication.py
# @Software: PyCharm
# @Motto: good good study,day day up

from django.contrib.auth.models import User


class EmailAuthBackend:
    """
     Authenticate using an e-mail address.
    """

    def authenticate(self, request, username=None, password=None):
        try:
            user = User.objects.get(email=username)
            if user.check_password(password):
                return user
            return None
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(id=user_id)
        except User.DoesNotExist:
            return None
